package com.mmlc.meimei.lichengkongjian.home;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.DefaultHandler;
import com.mmlc.businessutils.MyShareUtils;
import com.mmlc.inter.InterActivity;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.utils.Constants;
import com.mmlc.utils.ToastUtils;
import com.mmlc.utils.ViewUtils;
import com.mmlc.utils.WebUtils;

public class WebViewActivity extends InterActivity {

    @Override
    protected int initContentView() {
        getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
        return R.layout.activity_web_view;
    }

    String title = "", htmlUrl = "", shareImageUrl, id;
    int type = 0, shareType = 0;

    @Override

    protected void initExtra() {
        super.initExtra();

        title = getIntent().getStringExtra("title");
        htmlUrl = getIntent().getStringExtra("url");
        shareImageUrl = getIntent().getStringExtra("shareImageUrl");
        if(!TextUtils.isEmpty(shareImageUrl)){
            shareImageUrl = htmlUrl;
        }
        id = getIntent().getStringExtra("id");
        type = getIntent().getIntExtra("type", 0);
        shareType = getIntent().getIntExtra("shareType", 0);
    }

    public String getLocalTitle() {
        return title;
    }

    public void setLocalTitle(String title) {
        this.title = title;
    }

    TextView tv_title;
    ImageView iv_share;
    BridgeWebView webView;
    ProgressBar progressBar;

    @Override
    protected void initView() {
        tv_title = ViewUtils.getView(activity, R.id.tv_title);
        iv_share = ViewUtils.getView(activity, R.id.iv_share);
        webView = ViewUtils.getView(activity, R.id.webview);
        progressBar = ViewUtils.getView(activity, R.id.progress);

        if (type == 1) {
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) webView.getLayoutParams();//new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 0, 0, 0);
            webView.setLayoutParams(params);
        }
        if (shareType == 1) {
            iv_share.setVisibility(View.GONE);
        }
        if (!ViewUtils.isEmpty(title)) tv_title.setText(title);

        webView.setDefaultHandler(new DefaultHandler());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.setWebChromeClient(new WebChromeClient());
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setDisplayZoomControls(true);
//        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setUserAgentString(webView.getSettings().getUserAgentString() + "mmlc.meiqi");
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
        webView.getSettings().setAppCacheEnabled(true);
        webView.getSettings().setAppCachePath(Constants.DIR_CACHE);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setDatabaseEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);

//        webView.setWebViewClient(new WebViewClient() {
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                if (htmlUrl.startsWith("http:") || htmlUrl.startsWith("https:")) {
//                    return false;
//                }
//                // 返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
//                //view.loadUrl(htmlUrl);
//                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(htmlUrl));
//                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                activity.startActivity(intent);
//                return true;
//            }
//
//        });
        webView.requestFocus();
        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

        webView.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                if (ViewUtils.isEmpty(getLocalTitle())) {
                    setLocalTitle(title);
                    tv_title.setText(title);
                }
            }

            @Override
            public void onProgressChanged(WebView view, int progress) {
                progressBar.setProgress(progress);
                if (progress == 100)
                    progressBar.setVisibility(View.GONE);
                else
                    progressBar.setVisibility(View.VISIBLE);
            }
        });
        WebUtils.setWebJS(webView,this,tv_title);
        if (ViewUtils.isEmpty(htmlUrl) && !ViewUtils.isEmpty(id))
            htmlUrl = "http://wx.lolever.com/html/info_details.html?id=" + id;

        if (!ViewUtils.isEmpty(htmlUrl)) {
//        "http://security.tencent.com/lucky/check_tools.html"
            webView.loadUrl(htmlUrl);
        }else{
            ToastUtils.showToast("网络请求有误");
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        webView.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        webView.onResume();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ViewUtils.getView(activity, R.id.iv_back).setOnClickListener(this);
        ViewUtils.getView(activity, R.id.iv_share).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_share:
                initShare();
                break;
        }
    }

/*    UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA share_media) {
            LogUtils.e("umShareListener", share_media.toString() + "-->分享成功");
        }

        @Override
        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
            LogUtils.e("umShareListener", share_media.toString() + "-->分享失败");
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media) {
            LogUtils.e("umShareListener", share_media.toString() + "-->取消分享");
        }
    };*/

    /**
     * 分享
     */
    private void initShare() {
   /*     final SHARE_MEDIA[] displaylist = new SHARE_MEDIA[]
                {
                        SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE,// SHARE_MEDIA.SINA,
                        SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE
                };

        UMImage umImage = ViewUtils.isEmpty(shareImageUrl) ? new UMImage(context,
                BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                : new UMImage(context, shareImageUrl);
        new ShareAction(this).setDisplayList(displaylist)
                .setListenerList(umShareListener)
                .withTitle(title)
                .withText(title)
                .withTargetUrl(htmlUrl)
                .withMedia(umImage)
                .open();*/
        showShare();
    }

    private void showShare() {
        MyShareUtils.showShare(title, title, htmlUrl, shareImageUrl);
        /*if(ViewUtils.isEmpty(shareImageUrl) ){
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
            String absolutePath = Environment.getExternalStorageDirectory().getAbsolutePath();
            String filePath = absolutePath+File.separator+"sharedImg.png";
            FileOutputStream output = null;
            try {
                output = new FileOutputStream(filePath);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            bitmap.compress(Bitmap.CompressFormat.PNG,100,output);
            oks.setImagePath(filePath);
        }*/
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /*UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);*/
    }


}
